var t = require("../../../@babel/runtime/helpers/interopRequireDefault")(require("../../../@babel/runtime/regenerator")), o = require("../../../@babel/runtime/helpers/asyncToGenerator"), e = getApp(), s = require("../../../mybehaviors/location/location");

Page({
    behaviors: [ s ],
    data: {
        locationStatus: !1,
        couponName: "",
        beginStatus: 0,
        storeName: "",
        storeDetail: {},
        goodsId: 0,
        addressDetail: "",
        imageRight: !1,
        favorite: !1,
        shareShow: !1,
        posterImg: "",
        posterShow: !1,
        goodsDetails: {},
        goodsPlugin: {},
        goodsBanner: "",
        couponStoreGoods: {},
        getGoods: {},
        fightGroupsId: 0,
        pluginSetting: {},
        customerOpen: !1,
        serviceShow: !1,
        serviceQRcode: "",
        serviceMobile: "",
        cservice: "",
        storeListByGoods: [],
        longitude: "",
        latitude: "",
        activity_id: "",
        discountWidth: 0,
        total: 0
    },
    onLoad: function(t) {
        var o, e = this;
        "" !== (null !== (o = t.goodsId) && void 0 !== o ? o : "") && (this.setData({
            goodsId: parseInt(t.goodsId)
        }), this.getGoodsDetails(this.data.goodsId)), this.goodsPoster();
        try {
            wx.removeStorageSync("applyaddresspoint");
        } catch (t) {}
        this._getLocation(function(t, o) {
            e.setData({
                longitude: o.lng,
                latitude: o.lat
            }), e.getStoreListByGoods();
        }, function(t) {
            e.setData({
                locationStatus: t
            });
        }), this.serviceDetail();
    },
    onReady: function() {},
    onShow: function() {
        var t = this;
        try {
            var o = wx.getStorageSync("applyaddresspoint");
            o ? this.setData({
                longitude: o.point.lng,
                latitude: o.point.lat,
                locationStatus: !0
            }) : this._getLocation(function(o, e) {
                t.setData({
                    longitude: e.lng,
                    latitude: e.lat,
                    locationStatus: !0
                }), t.getStoreListByGoods();
            }, function(o) {
                t.setData({
                    locationStatus: o
                });
            });
        } catch (t) {
            console.log(t);
        }
        wx.showShareMenu({
            withShareTicket: !1,
            menus: [ "shareAppMessage", "shareTimeline" ]
        });
    },
    getStoreListByGoods: function() {
        var t = this, o = e.getNetAddresss("plugin.coupon-store.frontend.store.getStoreListByGoods"), s = {
            goods_id: this.data.goodsId,
            store_name: "",
            longitude: this.data.longitude,
            latitude: this.data.latitude
        };
        e._postNetWork({
            url: o,
            data: s,
            success: function(o) {
                var e = o.data;
                if (1 == e.result) {
                    var s = e.data.data, a = "".concat(s[0].city).concat(s[0].district).concat(s[0].street).concat(s[0].address);
                    t.setData({
                        addressDetail: a,
                        storeListByGoods: e.data.data,
                        total: e.data.total
                    });
                } else wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getGoodsDetails: function(t) {
        var o = this, s = e.getNetAddresss("goods.goods.get-goods-page"), a = {
            id: Number(t),
            basic_info: 1
        };
        e._postNetWork({
            url: s,
            data: a,
            success: function(t) {
                var e, s = t.data, a = null === (e = s.data.plugin) || void 0 === e ? void 0 : e.coupon_store_goods, i = 1e3 * a.start_time, n = 1e3 * a.end_time, r = new Date().getTime(), c = 0;
                if (c = i > r ? 0 : r >= i && r < n ? 1 : 2, 1 == s.result) {
                    var d, u, l, g, h;
                    o.setData({
                        goodsBanner: s.data.get_goods.thumb,
                        fightGroupsId: null === (d = s.data.plugin.coupon_store_goods) || void 0 === d ? void 0 : d.fight_groups_id,
                        couponStoreGoods: null === (u = s.data.plugin) || void 0 === u ? void 0 : u.coupon_store_goods,
                        getGoods: s.data.get_goods,
                        goodsPlugin: s.data.plugin,
                        beginStatus: c,
                        pluginSetting: null === (l = s.basic_info) || void 0 === l || null === (g = l.plugin_setting) || void 0 === g ? void 0 : g.coupon_store,
                        favorite: !(null === (h = s.data.is_favorite) || void 0 === h || !h.status)
                    });
                    var p = o.createSelectorQuery(), f = o;
                    p.select("#goods-price").boundingClientRect(), p.exec(function(t) {
                        f.setData({
                            discountWidth: t[0].width
                        });
                    });
                }
            }
        });
    },
    toGroupCode: function() {
        "home" === this.data.goodsPlugin.group_code.jump_mode ? wx.reLaunch({
            url: "/packageE/groupCode/groupCode"
        }) : this.data.goodsPlugin.group_code.code_mini_url && wx.navigateTo({
            url: this.data.goodsPlugin.group_code.code_mini_url
        });
    },
    shopNavTo: function() {
        wx.navigateTo({
            url: "/packageF/consumption/shopList/shopList?goodsId=".concat(this.data.goodsId, "&storeName=").concat(this.data.storeDetail.store_name, "&longitude=").concat(this.data.longitude, "&latitude=").concat(this.data.latitude)
        });
    },
    locationOn: function() {
        if (!this.checkAlbum("scope.userLocation")) return e.tips("获取位置失败，请重新进入后授权");
    },
    checkAlbum: function(e) {
        var s = this;
        return o(t.default.mark(function o() {
            var a;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, s.getAuthSetting(e);

                  case 2:
                    return a = t.sent, t.abrupt("return", new Promise(function(t, o) {
                        a ? t(!0) : wx.showModal({
                            title: "请开启授权",
                            content: "是否跳转设置页开启授权",
                            success: function(o) {
                                o.confirm ? wx.openSetting({
                                    success: function(o) {
                                        o.authSetting[e] ? t(!0) : t(!1);
                                    }
                                }) : o.cancel && t(!1);
                            },
                            fail: function() {
                                t(!1);
                            }
                        });
                    }));

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, o);
        }))();
    },
    getAuthSetting: function(t) {
        return new Promise(function(o, e) {
            wx.getSetting({
                success: function(e) {
                    e.authSetting[t] ? o(!0) : wx.authorize({
                        scope: t,
                        success: function() {
                            o(!0);
                        },
                        fail: function() {
                            o(!1);
                        }
                    });
                },
                fail: function() {
                    o(!1);
                }
            });
        });
    },
    onPreorder: function() {
        0 !== this.data.getGoods.show_stock ? wx.navigateTo({
            url: "/packageF/consumption/orderAffirm/orderAffirm?goodsId=".concat(this.data.goodsId, "&optionsId=&total=1")
        }) : wx.showToast({
            icon: "none",
            title: "商品没有库存了",
            duration: 1500
        });
    },
    goodsGroup: function() {
        0 !== this.data.getGoods.show_stock ? wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=".concat(this.data.couponStoreGoods.fight_groups_id, "&name=group")
        }) : wx.showToast({
            icon: "none",
            title: "商品没有库存了",
            duration: 1500
        });
    },
    onFavorite: function() {
        var t = this, o = "", s = !this.data.favorite;
        o = s ? e.getNetAddresss("member.member-favorite.store") : e.getNetAddresss("member.member-favorite.destroy"), 
        o += "&goods_id=" + this.data.goodsId, e._getNetWork({
            url: o,
            success: function(o) {
                var e = o.data;
                t.setData({
                    favorite: s
                }), e.result, wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 3e3
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    shareHint: function() {
        wx.showToast({
            icon: "none",
            title: "长按图片分享好友",
            duration: 3e3
        });
    },
    goodsPoster: function() {
        var t = this, o = e.getNetAddresss("plugin.coupon-store.frontend.poster.goodsPoster"), s = {
            goods_id: this.data.goodsId
        };
        e._postNetWork({
            url: o,
            data: s,
            success: function(o) {
                var e = o.data;
                1 == e.result ? t.setData({
                    posterImg: e.data.url
                }) : wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    tapIndex: function() {
        wx.redirectTo({
            url: "/packageG/index/index"
        });
    },
    gotoChat: function(t) {
        console.log(t);
        var o = t.currentTarget.dataset.cservice + "&goods_id=" + this.data.goodsId;
        wx.navigateTo({
            url: o,
            fail: function(t) {
                e.tips("客服路由出错");
            }
        });
    },
    serviceDetail: function() {
        var t = this, o = this.data.goodsId, s = e.getNetAddresss("goods.customer-service.index");
        e._postNetWork({
            url: s,
            data: {
                goods_id: o
            },
            success: function(o) {
                var e = o.data;
                1 == e.result && (t.setData({
                    customerOpen: e.data.customer_open,
                    showKefu: !0,
                    cservice: e.data.cservice,
                    serviceQRcode: e.data.service_QRcode,
                    serviceMobile: e.data.service_mobile
                }), console.log(t.data.cservice));
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    saveImg: function() {
        wx.getSetting({
            success: function(t) {
                t.authSetting["scope.writePhotosAlbum"] || wx.authorize({
                    scope: "scope.writePhotosAlbum",
                    success: function() {
                        console.log("授权成功");
                    }
                });
            }
        });
        var t = this.data.posterImg;
        wx.downloadFile({
            url: t,
            success: function(t) {
                wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath,
                    success: function(t) {
                        wx.showToast({
                            title: "保存成功",
                            icon: "none",
                            duration: 2e3
                        });
                    },
                    fail: function(t) {
                        "saveImageToPhotosAlbum:fail auth deny" === t.errMsg && (console.log("当初用户拒绝，再次发起授权"), 
                        wx.openSetting({
                            success: function(t) {
                                t.authSetting["scope.writePhotosAlbum"] ? wx.showToast({
                                    icon: "none",
                                    title: "获取权限成功,再次长按保存图片",
                                    duration: 1500
                                }) : wx.showToast({
                                    icon: "none",
                                    title: "获取权限失败,保存图片需要用户授权",
                                    duration: 1500
                                });
                            }
                        }));
                    },
                    complete: function(t) {
                        console.log(t);
                    }
                });
            }
        });
    },
    showPop: function() {
        console.log("12342");
        var t = e._isTextEmpty(this.data.serviceQRcode) && e._isTextEmpty(this.data.serviceMobile);
        if (e._isTextEmpty(this.data.cservice) && t) return e.tips("抱歉，暂未设置客服信息");
        e._isTextEmpty(this.data.cservice) || !t ? this.setData({
            serviceShow: !0
        }) : this.gotoChat({
            currentTarget: {
                dataset: {
                    cservice: this.data.cservice
                }
            }
        });
    },
    sharePopup: function() {
        this.setData({
            shareShow: !0
        });
    },
    closePop: function() {
        this.setData({
            serviceShow: !1
        });
    },
    PosterClose: function() {
        this.setData({
            posterShow: !1
        });
    },
    shareClose: function() {
        this.setData({
            shareShow: !1
        });
    },
    PosterPopup: function() {
        this.setData({
            posterShow: !0
        });
    },
    callPhone: function() {
        wx.makePhoneCall({
            phoneNumber: this.data.serviceMobile
        });
    },
    callStore: function() {
        wx.makePhoneCall({
            phoneNumber: this.data.storeListByGoods[0].store_mobile
        });
    },
    copyWriting: function() {
        wx.navigateTo({
            url: "/packageF/consumption/copyWriting/copyWriting?goodsId=" + this.data.goodsId
        });
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareTimeline: function() {
        return {
            title: this.data.getGoods.title,
            query: "/packageF/consumption/goodsDetails/goodsDetails?goodsId=".concat(this.data.goodsId),
            imageUrl: this.data.goodsBanner
        };
    },
    onShareAppMessage: function() {
        var t = {
            title: this.data.getGoods.title,
            path: "/packageF/consumption/goodsDetails/goodsDetails?goodsId=".concat(this.data.goodsId),
            imageUrl: this.data.goodsBanner
        };
        return {
            title: t.title,
            path: t.path,
            imageUrl: t.imgUrl
        };
    }
});